﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Amazon;
using Amazon.AutoScaling;
using Amazon.AutoScaling.Model;

namespace Web_App___AWS_ELB_Monitor
{
    public static class ASDataHelper
    {

        public static AutoScalingGroup GetAS(string name)
        {
            var response = GetASClient().DescribeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().WithAutoScalingGroupNames(name));
            if (response.DescribeAutoScalingGroupsResult == null)
                throw new Exception("Request for auto scaling group did not result in a proper result. Was there a previous exception?");
            return response.DescribeAutoScalingGroupsResult.AutoScalingGroups.FirstOrDefault();
        }

        public static List<AutoScalingGroup> GetAllASGroups()
        {
            var response = GetASClient().DescribeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().WithMaxRecords(30));
            if (response.DescribeAutoScalingGroupsResult == null)
                throw new Exception("Request for auto scaling group did not result in a proper result. Was there a previous exception?");
            return response.DescribeAutoScalingGroupsResult.AutoScalingGroups;
        }

        public static AmazonAutoScaling GetASClient()
        {
            return AWSClientFactory.CreateAmazonAutoScalingClient(CustomValueHelper.GetProperty("AWSAccessKey"),
                                                                  CustomValueHelper.GetProperty("AWSSecretKey"),
                                                                  new AmazonAutoScalingConfig { ServiceURL = "https://autoscaling.eu-west-1.amazonaws.com" });

        }
    }
}